﻿package mortal.game.view.task.drama.operations.npctalk
{
    import com.gengine.debug.*;
    import com.gengine.global.*;
    import com.mui.controls.*;
    import com.mui.utils.*;
    import flash.events.*;
    import flash.text.*;
    import flash.utils.*;
    import mortal.common.*;
    import mortal.common.swfPlayer.*;
    import mortal.common.swfPlayer.data.*;
    import mortal.game.view.common.*;

    public class TaskDramaTalkMaskText extends GSprite
    {
        private var _data:TaskDramaTalkData;
        private var _txtTalk:GTextFiled;
        private var _mask:TaskDramaTalkMask;
        private var _onEnd:Function;
        private var _timerId1:int;
        private var _timerId2:int;
        private var _facesContainer:GSprite;
        private var _textFaceContainer:GSprite;
        private var _txtFaceSp:GSprite;
        private var _isShowingStage:Boolean;
        private var _movies:Array;
        private var _faceParseRes:Array;

        public function TaskDramaTalkMaskText()
        {
            this._movies = [];
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            this._data = null;
            this._txtTalk.dispose(param1);
            this._txtTalk = null;
            this._mask.dispose(param1);
            this._mask = null;
            this._onEnd = null;
            if (this._timerId1 > 0)
            {
                clearTimeout(this._timerId1);
                this._timerId1 = -1;
            }
            if (this._timerId2 > 0)
            {
                clearTimeout(this._timerId2);
                this._timerId2 = -1;
            }
            this._facesContainer.dispose(param1);
            this._facesContainer = null;
            this._textFaceContainer.dispose(param1);
            this._textFaceContainer = null;
            if (this._txtFaceSp != null)
            {
                this._txtFaceSp.dispose(param1);
                this._txtFaceSp = null;
            }
            this._isShowingStage = false;
            this.disposeMovies();
            this._faceParseRes = null;
            Global.stage.removeEventListener(MouseEvent.CLICK, this.clickNextStep);
            return;
        }// end function

        public function get data() : TaskDramaTalkData
        {
            return this._data;
        }// end function

        public function resize(param1:int) : void
        {
            this._mask.reWidth(param1);
            this._txtTalk.width = param1;
            return;
        }// end function

        public function show(param1:TaskDramaTalkData, param2:Function, param3:Boolean = true) : void
        {
            this._data = param1;
            this._onEnd = param2;
            this._isShowingStage = false;
            var _loc_4:* = this._txtTalk.defaultTextFormat;
            _loc_4.size = this._data.talkFontSize;
            _loc_4.leading = this._data.talkFontLeading;
            this._txtTalk.defaultTextFormat = _loc_4;
            this._txtTalk.textColor = 11795964;
            this._txtTalk.width = this._data.rowWidth;
            this._txtTalk.text = "";
            if (param3)
            {
                Global.stage.addEventListener(MouseEvent.CLICK, this.clickNextStep);
            }
            if (this._timerId1 > 0)
            {
                clearTimeout(this._timerId1);
            }
            if (this._data.popupTime > 0)
            {
                this._timerId1 = setTimeout(this.onPopupEnd, this._data.popupTime * 1000);
            }
            else
            {
                this.onPopupEnd();
            }
            return;
        }// end function

        private function clickNextStep(event:MouseEvent) : void
        {
            if (!this._isShowingStage)
            {
                clearTimeout(this._timerId1);
                this._timerId1 = -1;
                this._mask.finishRightNow();
            }
            else
            {
                clearTimeout(this._timerId2);
                this.onShowEnd();
            }
            return;
        }// end function

        private function onPopupEnd() : void
        {
            Log.error("..........xxxx........TaskDramaTalkMaskText, onPopupEnd");
            this._timerId1 = -1;
            this._faceParseRes = TaskDramaTalkTextParser.instance.parse(this._data.talk, this._data.talkFontLeading, this._data.talkFontSize, this._data.rowWidth);
            this.loadVipFace();
            return;
        }// end function

        private function loadVipFace() : void
        {
            var _loc_1:* = this._faceParseRes[2];
            this.loadNormalFace();
            return;
        }// end function

        private function loadNormalFace() : void
        {
            Log.error("..........xxxx........TaskDramaTalkMaskText, loadNormalFace");
            var _loc_1:* = this._faceParseRes[3];
            this.addFace();
            return;
        }// end function

        private function addFace() : void
        {
            this._txtTalk.htmlText = String(this._faceParseRes[0]);
            Log.error("..........xxxx........TaskDramaTalkMaskText, addFace,开始滚动字幕, _txtTalk = " + this._txtTalk.text);
            DisplayUtil.removeAllChild(this._facesContainer);
            this._facesContainer.addChild(this.getFaceSprite(this._faceParseRes[1] as Array));
            this._mask.reInit(this._data, this._txtTalk, this.onRollingEnd);
            this._isShowingStage = false;
            return;
        }// end function

        private function disposeMovies() : void
        {
            var _loc_1:SWFPlayer = null;
            if (this._movies == null || this._movies.length == 0)
            {
                return;
            }
            for each (_loc_1 in this._movies)
            {
                
                _loc_1.dispose();
            }
            this._movies = [];
            return;
        }// end function

        private function getFaceSprite(param1:Array) : GSprite
        {
            var _loc_3:TaskDramaTalkFaceData = null;
            var _loc_4:String = null;
            var _loc_5:SWFPlayer = null;
            this.disposeMovies();
            if (this._txtFaceSp == null)
            {
                this._txtFaceSp = UICompomentPool.getUICompoment(GSprite) as GSprite;
            }
            var _loc_2:int = 0;
            while (param1 && _loc_2 < param1.length)
            {
                
                _loc_3 = param1[_loc_2] as TaskDramaTalkFaceData;
                _loc_4 = _loc_3.faceId;
                if (_loc_4.indexOf("b") < 0)
                {
                    _loc_4 = "a" + _loc_3.faceId;
                }
                _loc_5 = UIFactory.getUICompoment(SWFPlayer);
                _loc_5.timeRate = 2;
                _loc_5.move(0, 0);
                _loc_5.load(_loc_4 + ".swf", ModelType.NormalSwf, null);
                this._txtFaceSp.addChild(_loc_5);
                this._movies.push(_loc_5);
                _loc_2++;
            }
            return this._txtFaceSp;
        }// end function

        private function onRollingEnd() : void
        {
            this._isShowingStage = true;
            this._timerId2 = setTimeout(this.onShowEnd, this._data.showTime);
            return;
        }// end function

        private function onShowEnd() : void
        {
            if (this._onEnd != null)
            {
                this._onEnd.apply();
                this._onEnd = null;
            }
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this._textFaceContainer = UICompomentPool.getUICompoment(GSprite) as GSprite;
            this.addChild(this._textFaceContainer);
            this._txtTalk = UIFactory.gTextField("", 0, 0, 200, 500, this._textFaceContainer);
            this._txtTalk.multiline = true;
            this._txtTalk.wordWrap = true;
            this._txtTalk.mouseEnabled = false;
            this._txtTalk.mouseWheelEnabled = false;
            this._facesContainer = UICompomentPool.getUICompoment(GSprite) as GSprite;
            this._textFaceContainer.addChild(this._facesContainer);
            this._mask = UICompomentPool.getUICompoment(TaskDramaTalkMask) as TaskDramaTalkMask;
            this._textFaceContainer.mask = this._mask;
            this.addChild(this._mask);
            return;
        }// end function

    }
}
